package com.sanyu.lovefishing.ui.adapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.collection.SparseArrayCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import com.sanyu.lovefishing.ui.fragment.WeatherFragment;

public class MainFragmentAdapter extends FragmentPagerAdapter {


    private String [] mTitles = new String[]{"指数", "天气", "地图", "百科"};
    private SparseArrayCompat<Fragment> mFragments = new SparseArrayCompat<>();

    public MainFragmentAdapter(@NonNull FragmentManager fm) {
        super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        Fragment fragment = mFragments.get(position);
        if (fragment==null){
            switch (position){
                default:
                case 0:
                    fragment = WeatherFragment.newInstance();
                    break;
                case 1:
                    fragment = WeatherFragment.newInstance();
                    break;
                case 2:
                    fragment = WeatherFragment.newInstance();
                    break;
                case 3:
                    fragment = WeatherFragment.newInstance();
                    break;
            }
            mFragments.put(position, fragment);
        }
        return fragment;
    }

    @Override
    public int getCount() {
        return mTitles.length;
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return mTitles[position];
    }
}
